//
//  BSMotion.m
//  BASE_LIB
//
//  Created by shiji on 2018/10/27.
//  Copyright © 2018年 shiji. All rights reserved.
//

#import "BSMotion.h"
#import <CoreMotion/CoreMotion.h>

@interface BSMotion ()
@property (nonatomic, strong) CMMotionManager *motionManager;
@end

@implementation BSMotion
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.motionManager = [[CMMotionManager alloc]init];
        self.accelerometerInterval = 0.01;
        self.gyroInterval = 0.01;
        self.magnetometerInterval = 0.01;
        
    }
    return self;
}

+ (instancetype)shared {
    static BSMotion *m = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        m = [BSMotion new];
    });
    return m;
}

// accelerometer
- (BOOL)isAccelerometerAvailable {
    return self.motionManager.isAccelerometerAvailable;
}
- (BOOL)isAccelerometerActive {
    return self.motionManager.isAccelerometerActive;
}
- (void)startAccelerometerUpdates {
    [self.motionManager startAccelerometerUpdates];
}
- (void)startAccelerometer:(NSOperationQueue *)queue withHandler:(CMAccelerometerHandler)handler {
    [self.motionManager startAccelerometerUpdatesToQueue:queue withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
        handler(accelerometerData, error);
    }];
}
- (void)stopAcceloremeter {
    [self.motionManager stopAccelerometerUpdates];
}
- (CMAccelerometerData *)accelerometerData {
    return self.motionManager.accelerometerData;
}


// groy
- (BOOL)isGroyAvailable {
    return self.motionManager.isGyroAvailable;
}
- (BOOL)isGyroActive {
    return self.motionManager.isGyroActive;
}
- (void)startGyroUpdates {
    [self.motionManager startGyroUpdates];
}
- (void)startGyro:(NSOperationQueue *)queue withHandler:(CMGyroHandler)handler {
    [self.motionManager startGyroUpdatesToQueue:queue withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {
        handler(gyroData, error);
    }];
}
- (void)stopGyro {
    [self.motionManager stopGyroUpdates];
}
- (CMGyroData *)gyroData {
    return self.motionManager.gyroData;
}


// magnetometer
- (BOOL)isMagnetometerAvailable {
    return self.motionManager.isMagnetometerAvailable;
}
- (BOOL)isMagnetometerActive {
    return self.motionManager.isMagnetometerActive;
}
- (void)startMagnetometerUpdates {
    [self.motionManager startMagnetometerUpdates];
}
- (void)startMagnetometer:(NSOperationQueue *)queue withHandler:(CMMagnetometerHandler)handler {
    [self.motionManager startMagnetometerUpdatesToQueue:queue withHandler:^(CMMagnetometerData * _Nullable magnetometerData, NSError * _Nullable error) {
        handler(magnetometerData, error);
    }];
}
- (void)stopMagnetometer {
    [self.motionManager stopMagnetometerUpdates];
}
- (CMMagnetometerData *)magnetometerData {
    return self.motionManager.magnetometerData;
}


// DeviceMotion
- (BOOL)isDeviceMotionAailable {
    return self.motionManager.deviceMotionAvailable;
}
- (BOOL)isDeviceMotionActive {
    return self.motionManager.isDeviceMotionActive;
}
- (void)startDeviceMotion {
    return [self.motionManager startDeviceMotionUpdates];
}
- (void)startDeviceMotion:(NSOperationQueue *)queue withHandler:(CMDeviceMotionHandler)handler {
    [self.motionManager startDeviceMotionUpdatesToQueue:queue withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error) {
        handler(motion, error);
    }];
}
- (void)stopDeviceMotion {
    [self.motionManager stopDeviceMotionUpdates];
}
- (CMDeviceMotion *)deviceMotion {
    return self.motionManager.deviceMotion;
}

@end
